contains_element() {
    local element match="$1"
    shift
    for element; do
        if [[ "$element" == "$match" ]]; then
            return 0
        fi
    done
    return 1
}
_xrngen-cli_autocomplete() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    all="${COMP_WORDS[@]}"
    opts="--png --flac "
    optprompt="-prompt -auto-prompt -no-prompt"
    optimage="-size -image"
    optaudio="-audio"
        
    # Convert space-separated strings to arrays
    read -r -a array1 <<< "${COMP_WORDS[@]}"
    
    
    if contains_element "--png" "${array1[@]}"; then
    	COMPREPLY=( $(compgen -W "-prompt -auto-prompt -no-prompt" -- ${cur}) )
    	#echo $array1
    	if  contains_element "-prompt" "${array1[@]}" ||  contains_element "-no-prompt" "${array1[@]}" || contains_element "-auto-prompt" "${array1[@]}"  ; then
    		if [[ ${prev} == "-image" ]] ;then 
	    		COMPREPLY=( "$dirpath"/* )
	    	else 
	    		COMPREPLY=( $(compgen -W "$optimage" -- ${cur}) )
	    	fi
	    	
    	else 
    		COMPREPLY=( $(compgen -W "-prompt -auto-prompt -no-prompt" -- ${cur}) )
    	fi
    fi
    if contains_element "--flac" "${array1[@]}"; then
    	COMPREPLY=( $(compgen -W "-prompt -auto-prompt" -- ${cur}) )
    	#echo $array1
    	if  contains_element "-prompt" "${array1[@]}" ||  contains_element "-auto-prompt" "${array1[@]}"  ; then
	    	if [[ ${prev} == "-audio" ]] ;then 
	    		COMPREPLY=( "$dirpath"/* )
	    	else 
	    		COMPREPLY=( $(compgen -W "$optaudio" -- ${cur}) )
	    	fi
	    	
	    	
    	else 
    			COMPREPLY=( $(compgen -W "-prompt -auto-prompt" -- ${cur}) )
    	fi
    fi
    
    if [[ ${prev} == "xrngen-cli" ]]; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    fi

}

complete -F _xrngen-cli_autocomplete xrngen-cli

